package com.trepadora.flex
{
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetStream;

	import mx.events.FlexEvent;
	import mx.events.SliderEvent;

	public class StreamPanel extends VideoPanel
	{
		private var ns:NetStream;

		public function init(streamId:String, volume:Number=1):void
		{
			_streamID=streamId;

			ns=new NetStream(AVManager.getNc());

			ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

			video.attachNetStream(ns);

			ns.soundTransform=new SoundTransform(volume);
			ns.play(streamID);

			onComplete();
			onSliderCreate();
			
			soundOffImage.visible = getSoundOffVisible();
			soundOnImage.visible = getSoundOnVisible();
		}

		public function getNs():NetStream
		{
			return ns;
		}

		public function clear():void
		{
			video=new Video(170, 135);
			videoDisplay.video=video;
			video.attachNetStream(null);
		}

		private function onComplete():void
		{
			trace("complete")
			if (_streamID != null && _streamID.indexOf("audio") != -1)
			{
				trace("slider init")
				initVolumeSlider();
				volumeSlider.maximum=1;
				volumeSlider.minimum=0;
				volumeSlider.liveDragging=true;
				volumeSlider.value=ns.soundTransform.volume;
				volumeSlider.addEventListener(SliderEvent.CHANGE, onChangeVolume);

				soundOffImage.buttonMode=true;
				soundOnImage.buttonMode=true;
				soundOffImage.addEventListener(MouseEvent.CLICK, onSoundOffClick);
				soundOnImage.addEventListener(MouseEvent.CLICK, onSoundOnClick);
			}
		}

		private function onChangeVolume(event:SliderEvent):void
		{
			var currentValue:Number=volumeSlider.value;

			if (currentValue == 0)
			{
				soundOffImage.visible=true;
				soundOnImage.visible=false;
			}
			else
			{
				soundOffImage.visible=false;
				soundOnImage.visible=true;
			}

			ns.soundTransform=new SoundTransform(currentValue);
		}


		private function onSliderCreate():void
		{
			if (_streamID != null && ns.soundTransform.volume == 0)
			{
				soundOnImage.visible=false;
				soundOffImage.visible=true;
			}
		}

		private function onNetStatus(event:NetStatusEvent):void
		{
			trace("EVENT " + event.info.code)
		}

		private function onSoundOffClick(event:MouseEvent):void
		{
			volumeSlider.value=1;
			onChangeVolume(null);
		}

		private function onSoundOnClick(event:MouseEvent):void
		{
			volumeSlider.value=0;
			onChangeVolume(null);
		}
	}
}